home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-11-14 | 2.8 KB | 91 lines | [TEXT/IGR0] |
-
- #include <DSP Window Functions>
- #include <BringDestToFront>
-
- | Given a long data wave create a short result wave containing the Power
- | Spectral Density. The name of the new wave is the name of the source + _psd
- | Note: if you don't want the baggage of all the window functions, create your own version
- | with your favorite and remove the window parameter (or choose a subset)
- | Also, note that a unity amplitude sine wave will give an integral peak size of
- | 0.5 rather than 0.707 as one would get if a real sine wave of 1 Volt peak amplitude
- | were applied across a 1 ohm resistor. Thus if you want physical meaning for
- | the results you should scale by 1.414/R where R is the actual resistance.
- |
- | Change 941114: Divided DC component by 2
- |
- Macro PSD(w,seglen,window)
- string w
- Prompt w "data wave:",popup WaveList("*",";","")
- variable seglen=1
- Prompt seglen,"segment length:",popup "256;512;1024;2048;4096;8192"
- variable window=2
- Prompt window,"Window type:",popup "Square;Hann;Parzen;Welch;Hamming;"
- "BlackmanHarris3;KaiserBessel"
- ;
- PauseUpdate; silent 1
-
- variable npsd= 2^(7+seglen) | number of points in group (resultant psd wave len= npsd/2+1)
- variable psdOffset= npsd/2 | offset each group by this amount
- variable psdFirst=0 | start of current group
- variable nsrc= numpnts($w)
- variable nsegs,winNorm | count of number of segements and window normalization factor
- string destw=w+"_psd",srctmp=w+"_tmp"
- string winw=w+"_psdWin" | window goes here
-
- if( npsd > nsrc/2 )
- Abort "psd: source wave should be MUCH longer than the segment length"
- endif
- make/o/n=(npsd/2+1) $destw
- make/o/n=(npsd) $srctmp,$winw; $winw= 1
- if( window==1 )
- winNorm= 1
- else
- if( window==2 )
- Hanning $winw;winNorm=0.372 | winNorm is avg squared value
- else
- if( window==3 )
- winNorm= Parzen($winw)
- else
- if( window==4 )
- winNorm= Welch($winw)
- else
- if( window==5 )
- winNorm= Hamming($winw)
- else
- if( window==6 )
- winNorm= BlackmanHarris3($winw)
- else
- if( window==7 )
- winNorm= KaiserBessel($winw)
- else
- Abort "unknown window index"
- endif
- endif
- endif
- endif
- endif
- endif
- endif | (kinda makes you wish we had elseif or switch constructs, huh?)
-
- Duplicate/O/R=[0,npsd-1] $w $srctmp; $srctmp *= $winw; fft $srctmp
- CopyScales/P $srctmp, $destw
- $destw= magsqr($srctmp)
- psdFirst= psdOffset
- nsegs=1
- do
- Duplicate/O/R=[psdFirst,psdFirst+npsd-1] $w $srctmp; $srctmp *= $winw
- fft $srctmp; $destw += magsqr($srctmp); psdFirst += psdOffset; nsegs+=1
- while( psdFirst+npsd < nsrc )
- winNorm= 2/(winNorm*nsegs*npsd^2); $destw *= winNorm
- $destw[0] /= 2
-
- KillWaves $srctmp,$winw
- BringDestFront(destw)
- if( numpnts($destw) <= 129 )
- Modify mode($destw)=4,marker($destw)=19,msize($destw)=1
- else
- Modify mode($destw)=0
- endif
- end
-
-